<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>Reranker Framework (ReFr): feature-vector.H Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />

<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">Reranker Framework (ReFr)
   
   </div>
   <div id="projectbrief">Reranking framework for structure prediction and discriminative language modeling</div>
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">feature-vector.H</div>  </div>
</div><!--header-->
<div class="contents">
<a href="feature-vector_8_h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// Copyright 2012, Google Inc.</span>
<a name="l00002"></a>00002 <span class="comment">// All rights reserved.</span>
<a name="l00003"></a>00003 <span class="comment">// </span>
<a name="l00004"></a>00004 <span class="comment">// Redistribution and use in source and binary forms, with or without</span>
<a name="l00005"></a>00005 <span class="comment">// modification, are permitted provided that the following conditions are</span>
<a name="l00006"></a>00006 <span class="comment">// met:</span>
<a name="l00007"></a>00007 <span class="comment">// </span>
<a name="l00008"></a>00008 <span class="comment">//   * Redistributions of source code must retain the above copyright</span>
<a name="l00009"></a>00009 <span class="comment">//     notice, this list of conditions and the following disclaimer.</span>
<a name="l00010"></a>00010 <span class="comment">//   * Redistributions in binary form must reproduce the above</span>
<a name="l00011"></a>00011 <span class="comment">//     copyright notice, this list of conditions and the following disclaimer</span>
<a name="l00012"></a>00012 <span class="comment">//     in the documentation and/or other materials provided with the</span>
<a name="l00013"></a>00013 <span class="comment">//     distribution.</span>
<a name="l00014"></a>00014 <span class="comment">//   * Neither the name of Google Inc. nor the names of its</span>
<a name="l00015"></a>00015 <span class="comment">//     contributors may be used to endorse or promote products derived from</span>
<a name="l00016"></a>00016 <span class="comment">//     this software without specific prior written permission.</span>
<a name="l00017"></a>00017 <span class="comment">//</span>
<a name="l00018"></a>00018 <span class="comment">// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS</span>
<a name="l00019"></a>00019 <span class="comment">// &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT</span>
<a name="l00020"></a>00020 <span class="comment">// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR</span>
<a name="l00021"></a>00021 <span class="comment">// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT</span>
<a name="l00022"></a>00022 <span class="comment">// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,</span>
<a name="l00023"></a>00023 <span class="comment">// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT</span>
<a name="l00024"></a>00024 <span class="comment">// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,           </span>
<a name="l00025"></a>00025 <span class="comment">// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY           </span>
<a name="l00026"></a>00026 <span class="comment">// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT</span>
<a name="l00027"></a>00027 <span class="comment">// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE</span>
<a name="l00028"></a>00028 <span class="comment">// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>
<a name="l00029"></a>00029 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00030"></a>00030 <span class="comment">//</span>
<a name="l00031"></a>00031 <span class="comment">//</span>
<a name="l00036"></a>00036 <span class="comment"></span>
<a name="l00037"></a>00037 <span class="preprocessor">#ifndef RERANKER_FEATURE_VECTOR_H_</span>
<a name="l00038"></a>00038 <span class="preprocessor"></span><span class="preprocessor">#define RERANKER_FEATURE_VECTOR_H_</span>
<a name="l00039"></a>00039 <span class="preprocessor"></span>
<a name="l00040"></a>00040 <span class="preprocessor">#include &lt;iostream&gt;</span>
<a name="l00041"></a>00041 <span class="preprocessor">#include &lt;map&gt;</span>
<a name="l00042"></a>00042 <span class="preprocessor">#include &lt;string&gt;</span>
<a name="l00043"></a>00043 <span class="preprocessor">#include &lt;tr1/unordered_map&gt;</span>
<a name="l00044"></a>00044 <span class="preprocessor">#include &lt;tr1/unordered_set&gt;</span>
<a name="l00045"></a>00045 <span class="preprocessor">#include &lt;vector&gt;</span>
<a name="l00046"></a>00046 
<a name="l00047"></a>00047 <span class="preprocessor">#include &quot;<a class="code" href="string-canonicalizer_8_h.html" title="Provides the reranker::StringCanonicalizer class.">string-canonicalizer.H</a>&quot;</span>
<a name="l00048"></a>00048 
<a name="l00049"></a>00049 <span class="keyword">namespace </span>reranker {
<a name="l00050"></a>00050 
<a name="l00051"></a>00051 <span class="keyword">using</span> std::cerr;
<a name="l00052"></a>00052 <span class="keyword">using</span> std::endl;
<a name="l00053"></a>00053 <span class="keyword">using</span> std::map;
<a name="l00054"></a>00054 <span class="keyword">using</span> std::ostream;
<a name="l00055"></a>00055 <span class="keyword">using</span> std::string;
<a name="l00056"></a>00056 <span class="keyword">using</span> std::tr1::unordered_map;
<a name="l00057"></a>00057 <span class="keyword">using</span> std::tr1::unordered_set;
<a name="l00058"></a>00058 
<a name="l00065"></a>00065 <span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt; 
<a name="l00066"></a><a class="code" href="structreranker_1_1_uid_getter.html">00066</a> <span class="keyword">struct </span><a class="code" href="structreranker_1_1_uid_getter.html" title="A simple class that provides a layer of abstraction when retrieving objects to represent unique ident...">UidGetter</a> {
<a name="l00068"></a><a class="code" href="structreranker_1_1_uid_getter.html#a2a79b591c1ac77d7cefbdda3fb92faf7">00068</a>   <span class="keyword">static</span> <span class="keyword">const</span> T &amp;<a class="code" href="structreranker_1_1_uid_getter.html#a2a79b591c1ac77d7cefbdda3fb92faf7" title="Gets the canonical uid object for the specified uid object.">Get</a>(<span class="keyword">const</span> T &amp;uid) { <span class="keywordflow">return</span> uid; }
<a name="l00069"></a>00069 };
<a name="l00070"></a>00070 
<a name="l00074"></a>00074 <span class="keyword">template</span>&lt;&gt;
<a name="l00075"></a><a class="code" href="structreranker_1_1_uid_getter_3_01string_01_4.html">00075</a> <span class="keyword">struct </span><a class="code" href="structreranker_1_1_uid_getter.html" title="A simple class that provides a layer of abstraction when retrieving objects to represent unique ident...">UidGetter</a>&lt;string&gt; {
<a name="l00077"></a><a class="code" href="structreranker_1_1_uid_getter_3_01string_01_4.html#a41212a53c12fc2befd238d6f048a6447">00077</a>   <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;<a class="code" href="structreranker_1_1_uid_getter_3_01string_01_4.html#a41212a53c12fc2befd238d6f048a6447" title="Gets the canonical uid string for the specified uid string.">Get</a>(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;uid) {
<a name="l00078"></a>00078     <span class="keywordflow">return</span> <a class="code" href="structreranker_1_1_uid_getter.html#a2a79b591c1ac77d7cefbdda3fb92faf7" title="Gets the canonical uid object for the specified uid object.">StringCanonicalizer::Get</a>(uid);
<a name="l00079"></a>00079   }
<a name="l00080"></a>00080 };
<a name="l00081"></a>00081 
<a name="l00090"></a>00090 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> K, <span class="keyword">typename</span> V, <span class="keyword">typename</span> Map = unordered_map&lt;K, V&gt; &gt;
<a name="l00091"></a><a class="code" href="classreranker_1_1_feature_vector.html">00091</a> <span class="keyword">class </span><a class="code" href="classreranker_1_1_feature_vector.html" title="A class to represent a feature vector, where features are represented by unique identifiers, and feature values are represented by the template type.">FeatureVector</a> {
<a name="l00092"></a>00092  <span class="keyword">public</span>:
<a name="l00094"></a><a class="code" href="classreranker_1_1_feature_vector.html#a038fac9f5a5d1bcfe98640eff66ba30c">00094</a>   <a class="code" href="classreranker_1_1_feature_vector.html#a038fac9f5a5d1bcfe98640eff66ba30c" title="Create an empty feature vector.">FeatureVector</a>() { }
<a name="l00095"></a>00095 
<a name="l00100"></a><a class="code" href="classreranker_1_1_feature_vector.html#a243fcd766c91f24bd79cbb6fb3aabe05">00100</a>   <a class="code" href="classreranker_1_1_feature_vector.html#a243fcd766c91f24bd79cbb6fb3aabe05" title="Copy features from the type of map used internally.">FeatureVector</a>(<span class="keyword">const</span> Map &amp;features) : features_(features) { }
<a name="l00101"></a>00101 
<a name="l00109"></a>00109   <span class="keyword">template</span> &lt;<span class="keyword">typename</span> MapType&gt;
<a name="l00110"></a><a class="code" href="classreranker_1_1_feature_vector.html#a299eb15548226ffe246a2fcdf78804d6">00110</a>   <a class="code" href="classreranker_1_1_feature_vector.html#a299eb15548226ffe246a2fcdf78804d6" title="Copy features from any map, or any collection of (feature,value) pairs, for that matter.">FeatureVector</a>(<span class="keyword">const</span> MapType &amp;features) {
<a name="l00111"></a>00111     <span class="keywordflow">for</span> (<span class="keyword">typename</span> MapType::const_iterator it = features.begin();
<a name="l00112"></a>00112          it != features.end(); ++it) {
<a name="l00113"></a>00113       features_.insert(*it);
<a name="l00114"></a>00114     } 
<a name="l00115"></a>00115   }
<a name="l00116"></a>00116 
<a name="l00117"></a><a class="code" href="classreranker_1_1_feature_vector.html#ae57362e810f7cf109f9c96153c387864">00117</a>   <span class="keyword">virtual</span> <a class="code" href="classreranker_1_1_feature_vector.html#ae57362e810f7cf109f9c96153c387864">~FeatureVector</a>() {}
<a name="l00118"></a>00118 
<a name="l00119"></a>00119   <span class="comment">// Forward type declarations that mirror what STL maps do.</span>
<a name="l00121"></a><a class="code" href="classreranker_1_1_feature_vector.html#ad77b756f443ca3cd938369dd0a5b617c">00121</a> <span class="comment"></span>  <span class="keyword">typedef</span> K <a class="code" href="classreranker_1_1_feature_vector.html#ad77b756f443ca3cd938369dd0a5b617c" title="The type of vector component/feature uid&#39;s in this vector.">key_type</a>;
<a name="l00123"></a><a class="code" href="classreranker_1_1_feature_vector.html#af1d4a320e7577f785791e5df70391264">00123</a>   <span class="keyword">typedef</span> V <a class="code" href="classreranker_1_1_feature_vector.html#af1d4a320e7577f785791e5df70391264" title="The type of values/feature weights in this vector.">mapped_type</a>;
<a name="l00124"></a>00124 
<a name="l00127"></a><a class="code" href="classreranker_1_1_feature_vector.html#acf6076a07743206afbc35acf99a8f2df">00127</a>   <span class="keyword">typedef</span> Map <a class="code" href="classreranker_1_1_feature_vector.html#acf6076a07743206afbc35acf99a8f2df" title="The underlying type that this class stores the mapping of feature uid&#39;s to their weights.">FeatureMap</a>;
<a name="l00129"></a><a class="code" href="classreranker_1_1_feature_vector.html#a8dc4093ae778bffb30d2840002f02ba1">00129</a>   <span class="keyword">typedef</span> <span class="keyword">typename</span> FeatureMap::const_iterator <a class="code" href="classreranker_1_1_feature_vector.html#a8dc4093ae778bffb30d2840002f02ba1" title="The type of const iterator for the feature-weight pairs in this vector.">const_iterator</a>;
<a name="l00130"></a>00130 
<a name="l00131"></a>00131   <span class="comment">// accessors</span>
<a name="l00132"></a>00132 
<a name="l00135"></a><a class="code" href="classreranker_1_1_feature_vector.html#ae0c185e646996daff25fcad9b224eda5">00135</a>   <a class="code" href="classreranker_1_1_feature_vector.html#a8dc4093ae778bffb30d2840002f02ba1" title="The type of const iterator for the feature-weight pairs in this vector.">const_iterator</a> <a class="code" href="classreranker_1_1_feature_vector.html#ae0c185e646996daff25fcad9b224eda5" title="Returns a const iterator pointing to the first of the feature-value pairs of this feature vector...">begin</a>()<span class="keyword"> const </span>{
<a name="l00136"></a>00136     <span class="keywordflow">return</span> features_.begin();
<a name="l00137"></a>00137   }
<a name="l00138"></a>00138 
<a name="l00141"></a><a class="code" href="classreranker_1_1_feature_vector.html#a9f2539faf4e600c2f75b613763d49fcd">00141</a>   <a class="code" href="classreranker_1_1_feature_vector.html#a8dc4093ae778bffb30d2840002f02ba1" title="The type of const iterator for the feature-weight pairs in this vector.">const_iterator</a> <a class="code" href="classreranker_1_1_feature_vector.html#a9f2539faf4e600c2f75b613763d49fcd" title="Returns a const iterator pointing to the end of the feature-value pairs of this feature vector...">end</a>()<span class="keyword"> const </span>{
<a name="l00142"></a>00142     <span class="keywordflow">return</span> features_.end();
<a name="l00143"></a>00143   }
<a name="l00144"></a>00144 
<a name="l00153"></a><a class="code" href="classreranker_1_1_feature_vector.html#a87bccd3acb055b98a674e6ed7d29af4b">00153</a>   unordered_set&lt;K&gt; &amp;<a class="code" href="classreranker_1_1_feature_vector.html#a87bccd3acb055b98a674e6ed7d29af4b" title="Inserts the uid&#39;s of features with non-zero weights into the specified set.">GetNonZeroFeatures</a>(unordered_set&lt;K&gt; &amp;<span class="keyword">set</span>)<span class="keyword"> const </span>{
<a name="l00154"></a>00154     <span class="keywordflow">for</span> (<a class="code" href="classreranker_1_1_feature_vector.html#a8dc4093ae778bffb30d2840002f02ba1" title="The type of const iterator for the feature-weight pairs in this vector.">const_iterator</a> it = features_.begin(); it != features_.end(); ++it) {
<a name="l00155"></a>00155       <span class="keyword">set</span>.insert(it-&gt;first);
<a name="l00156"></a>00156     }
<a name="l00157"></a>00157     <span class="keywordflow">return</span> <span class="keyword">set</span>;
<a name="l00158"></a>00158   }
<a name="l00159"></a>00159 
<a name="l00168"></a><a class="code" href="classreranker_1_1_feature_vector.html#ae4e7570b1829aa527fb6a487016437f0">00168</a>   unordered_set&lt;K&gt; &amp;<a class="code" href="classreranker_1_1_feature_vector.html#ae4e7570b1829aa527fb6a487016437f0" title="Removes the uid&#39;s of features with non-zero weights from the specified set.">RemoveNonZeroFeatures</a>(unordered_set&lt;K&gt; &amp;<span class="keyword">set</span>)<span class="keyword"> const </span>{
<a name="l00169"></a>00169     <span class="keywordflow">for</span> (<a class="code" href="classreranker_1_1_feature_vector.html#a8dc4093ae778bffb30d2840002f02ba1" title="The type of const iterator for the feature-weight pairs in this vector.">const_iterator</a> it = features_.begin(); it != features_.end(); ++it) {
<a name="l00170"></a>00170       <span class="keyword">set</span>.erase(it-&gt;first);
<a name="l00171"></a>00171     }
<a name="l00172"></a>00172     <span class="keywordflow">return</span> <span class="keyword">set</span>;    
<a name="l00173"></a>00173   }
<a name="l00174"></a>00174 
<a name="l00182"></a><a class="code" href="classreranker_1_1_feature_vector.html#a56cdaf41599879784f9342e6d94250b0">00182</a>   unordered_set&lt;K&gt; &amp;<a class="code" href="classreranker_1_1_feature_vector.html#a56cdaf41599879784f9342e6d94250b0" title="Removes from the specified set the uid&#39;s of feature with weights equal in this vector to their weight...">RemoveEqualFeatures</a>(<span class="keyword">const</span> <a class="code" href="classreranker_1_1_feature_vector.html" title="A class to represent a feature vector, where features are represented by unique identifiers, and feature values are represented by the template type.">FeatureVector&lt;K,V&gt;</a> &amp;other,
<a name="l00183"></a>00183                                         unordered_set&lt;K&gt; &amp;<span class="keyword">set</span>)<span class="keyword"> const </span>{
<a name="l00184"></a>00184     <span class="keywordflow">for</span> (<a class="code" href="classreranker_1_1_feature_vector.html#a8dc4093ae778bffb30d2840002f02ba1" title="The type of const iterator for the feature-weight pairs in this vector.">const_iterator</a> it = features_.begin(); it != features_.end(); ++it) {
<a name="l00185"></a>00185       <span class="keyword">const</span> K &amp;uid = it-&gt;first;
<a name="l00186"></a>00186       <span class="keywordflow">if</span> (<span class="keyword">set</span>.find(uid) != <span class="keyword">set</span>.end() &amp;&amp;
<a name="l00187"></a>00187           <a class="code" href="classreranker_1_1_feature_vector.html#a0786ae9b9f6b579a80d8ae601edfd80a" title="Returns the weight of the feature with the specified uid, where crucially features not &quot;present&quot; in t...">GetWeight</a>(uid) == other.<a class="code" href="classreranker_1_1_feature_vector.html#a0786ae9b9f6b579a80d8ae601edfd80a" title="Returns the weight of the feature with the specified uid, where crucially features not &quot;present&quot; in t...">GetWeight</a>(uid)) {
<a name="l00188"></a>00188         <span class="keyword">set</span>.erase(uid);
<a name="l00189"></a>00189       }
<a name="l00190"></a>00190     }
<a name="l00191"></a>00191     <span class="keywordflow">return</span> <span class="keyword">set</span>;
<a name="l00192"></a>00192   }
<a name="l00193"></a>00193 
<a name="l00199"></a><a class="code" href="classreranker_1_1_feature_vector.html#a0786ae9b9f6b579a80d8ae601edfd80a">00199</a>   V <a class="code" href="classreranker_1_1_feature_vector.html#a0786ae9b9f6b579a80d8ae601edfd80a" title="Returns the weight of the feature with the specified uid, where crucially features not &quot;present&quot; in t...">GetWeight</a>(<span class="keyword">const</span> K &amp;uid)<span class="keyword"> const </span>{
<a name="l00200"></a>00200     <a class="code" href="classreranker_1_1_feature_vector.html#a8dc4093ae778bffb30d2840002f02ba1" title="The type of const iterator for the feature-weight pairs in this vector.">const_iterator</a> feature_it = features_.find(uid);
<a name="l00201"></a>00201     <span class="keywordflow">return</span> feature_it == features_.end() ? V() : feature_it-&gt;second;
<a name="l00202"></a>00202   }
<a name="l00203"></a>00203 
<a name="l00207"></a><a class="code" href="classreranker_1_1_feature_vector.html#a565133f9b5b31de43d07aea909631be5">00207</a>   V <a class="code" href="classreranker_1_1_feature_vector.html#a565133f9b5b31de43d07aea909631be5" title="Synonymous with GetWeight.">GetValue</a>(<span class="keyword">const</span> K &amp;uid)<span class="keyword"> const </span>{
<a name="l00208"></a>00208     <span class="keywordflow">return</span> <a class="code" href="classreranker_1_1_feature_vector.html#a0786ae9b9f6b579a80d8ae601edfd80a" title="Returns the weight of the feature with the specified uid, where crucially features not &quot;present&quot; in t...">GetWeight</a>(uid);
<a name="l00209"></a>00209   }
<a name="l00210"></a>00210 
<a name="l00212"></a><a class="code" href="classreranker_1_1_feature_vector.html#a08a5b65bae6cc0bb35e035e5797a00f2">00212</a>   <span class="keywordtype">size_t</span> <a class="code" href="classreranker_1_1_feature_vector.html#a08a5b65bae6cc0bb35e035e5797a00f2" title="Returns the number of non-zero feature components of this feature vector.">size</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> features_.size(); }
<a name="l00213"></a>00213 
<a name="l00214"></a><a class="code" href="classreranker_1_1_feature_vector.html#a72b41f295541c52d9ad4a25e789b7ba4">00214</a>   <span class="keywordtype">bool</span> <a class="code" href="classreranker_1_1_feature_vector.html#a72b41f295541c52d9ad4a25e789b7ba4">empty</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> features_.empty(); }
<a name="l00215"></a>00215 
<a name="l00219"></a><a class="code" href="classreranker_1_1_feature_vector.html#a03929c2f26faf01b1406074943f99b86">00219</a>   V <a class="code" href="classreranker_1_1_feature_vector.html#a03929c2f26faf01b1406074943f99b86" title="Computes the dot product of this feature vector with the specified FeatureVector.">Dot</a>(<span class="keyword">const</span> <a class="code" href="classreranker_1_1_feature_vector.html" title="A class to represent a feature vector, where features are represented by unique identifiers, and feature values are represented by the template type.">FeatureVector&lt;K,V&gt;</a> &amp;other)<span class="keyword"> const </span>{
<a name="l00220"></a>00220     <span class="keywordtype">bool</span> this_is_smaller = <a class="code" href="classreranker_1_1_feature_vector.html#a08a5b65bae6cc0bb35e035e5797a00f2" title="Returns the number of non-zero feature components of this feature vector.">size</a>() &lt; other.<a class="code" href="classreranker_1_1_feature_vector.html#a08a5b65bae6cc0bb35e035e5797a00f2" title="Returns the number of non-zero feature components of this feature vector.">size</a>();
<a name="l00221"></a>00221     <span class="keyword">const</span> <a class="code" href="classreranker_1_1_feature_vector.html" title="A class to represent a feature vector, where features are represented by unique identifiers, and feature values are represented by the template type.">FeatureVector</a> &amp;small = this_is_smaller ? *<span class="keyword">this</span> : other;
<a name="l00222"></a>00222     <span class="keyword">const</span> <a class="code" href="classreranker_1_1_feature_vector.html" title="A class to represent a feature vector, where features are represented by unique identifiers, and feature values are represented by the template type.">FeatureVector</a> &amp;large = this_is_smaller ? other : *<span class="keyword">this</span>;
<a name="l00223"></a>00223     V dot_product = V();
<a name="l00224"></a>00224     <span class="keywordflow">for</span> (<a class="code" href="classreranker_1_1_feature_vector.html#a8dc4093ae778bffb30d2840002f02ba1" title="The type of const iterator for the feature-weight pairs in this vector.">const_iterator</a> it = small.features_.begin();
<a name="l00225"></a>00225          it != small.features_.end();
<a name="l00226"></a>00226          ++it) {
<a name="l00227"></a>00227       dot_product += it-&gt;second * large.<a class="code" href="classreranker_1_1_feature_vector.html#a0786ae9b9f6b579a80d8ae601edfd80a" title="Returns the weight of the feature with the specified uid, where crucially features not &quot;present&quot; in t...">GetWeight</a>(it-&gt;first);
<a name="l00228"></a>00228     }
<a name="l00229"></a>00229     <span class="keywordflow">return</span> dot_product;
<a name="l00230"></a>00230   }
<a name="l00231"></a>00231 
<a name="l00232"></a>00232   <span class="comment">// mutators</span>
<a name="l00233"></a>00233 
<a name="l00241"></a><a class="code" href="classreranker_1_1_feature_vector.html#a541f26184a4407f62eb5dad8102cf375">00241</a>   V <a class="code" href="classreranker_1_1_feature_vector.html#a541f26184a4407f62eb5dad8102cf375" title="Increments the weight of the specified feature by the specified amount.">IncrementWeight</a>(<span class="keyword">const</span> K &amp;uid, V by) {
<a name="l00242"></a>00242     <span class="keyword">const</span> K &amp;canonical_uid = <a class="code" href="structreranker_1_1_uid_getter.html" title="A simple class that provides a layer of abstraction when retrieving objects to represent unique ident...">UidGetter&lt;K&gt;::Get</a>(uid);
<a name="l00243"></a>00243     V default_val = V();  <span class="comment">// zero</span>
<a name="l00244"></a>00244     <span class="comment">// Short circuit: if we&#39;re trying to increase a feature&#39;s weight by</span>
<a name="l00245"></a>00245     <span class="comment">// zero, bail out early.</span>
<a name="l00246"></a>00246     <span class="keywordtype">bool</span> no_increase = by == default_val;
<a name="l00247"></a>00247     iterator feature_it = features_.find(canonical_uid);
<a name="l00248"></a>00248     <span class="keywordflow">if</span> (feature_it == features_.end()) {
<a name="l00249"></a>00249       <span class="keywordflow">if</span> (no_increase) {
<a name="l00250"></a>00250         <span class="keywordflow">return</span> default_val;
<a name="l00251"></a>00251       }
<a name="l00252"></a>00252       features_[canonical_uid] = by;
<a name="l00253"></a>00253       <span class="keywordflow">return</span> by;
<a name="l00254"></a>00254     } <span class="keywordflow">else</span> {
<a name="l00255"></a>00255       <span class="keywordflow">if</span> (no_increase) {
<a name="l00256"></a>00256         <span class="keywordflow">return</span> feature_it-&gt;second;
<a name="l00257"></a>00257       }
<a name="l00258"></a>00258       V new_weight = feature_it-&gt;second + by;
<a name="l00259"></a>00259       <span class="keywordflow">if</span> (new_weight == default_val) {
<a name="l00260"></a>00260         features_.erase(canonical_uid);
<a name="l00261"></a>00261       } <span class="keywordflow">else</span> {
<a name="l00262"></a>00262         feature_it-&gt;second = new_weight;
<a name="l00263"></a>00263       }
<a name="l00264"></a>00264       <span class="keywordflow">return</span> new_weight;
<a name="l00265"></a>00265     }
<a name="l00266"></a>00266   }
<a name="l00267"></a>00267 
<a name="l00276"></a><a class="code" href="classreranker_1_1_feature_vector.html#a2829655419a2dfd5014094e6834836df">00276</a>   V <a class="code" href="classreranker_1_1_feature_vector.html#a2829655419a2dfd5014094e6834836df" title="Increments the value of the specified vector component by the specified amount.">IncrementValue</a>(<span class="keyword">const</span> K &amp;uid, V by) {
<a name="l00277"></a>00277     <span class="keywordflow">return</span> <a class="code" href="classreranker_1_1_feature_vector.html#a541f26184a4407f62eb5dad8102cf375" title="Increments the weight of the specified feature by the specified amount.">IncrementWeight</a>(uid, by);
<a name="l00278"></a>00278   }
<a name="l00279"></a>00279 
<a name="l00284"></a><a class="code" href="classreranker_1_1_feature_vector.html#ad94d388ef81023981d6924a2020268d2">00284</a>   V <a class="code" href="classreranker_1_1_feature_vector.html#ad94d388ef81023981d6924a2020268d2" title="Sets the weight of the specified feature to the specified value.">SetWeight</a>(<span class="keyword">const</span> K &amp;uid, V new_weight) {
<a name="l00285"></a>00285     V old_weight = <a class="code" href="classreranker_1_1_feature_vector.html#a0786ae9b9f6b579a80d8ae601edfd80a" title="Returns the weight of the feature with the specified uid, where crucially features not &quot;present&quot; in t...">GetWeight</a>(uid);
<a name="l00286"></a>00286     <span class="keywordflow">if</span> (new_weight == V()) {
<a name="l00287"></a>00287       features_.erase(uid);
<a name="l00288"></a>00288     } <span class="keywordflow">else</span> {
<a name="l00289"></a>00289       features_[uid] = new_weight;
<a name="l00290"></a>00290     }
<a name="l00291"></a>00291     <span class="keywordflow">return</span> old_weight;
<a name="l00292"></a>00292   }
<a name="l00293"></a>00293 
<a name="l00295"></a><a class="code" href="classreranker_1_1_feature_vector.html#ac0a4fceef14eba9efb471f5ec0476579">00295</a>   V <a class="code" href="classreranker_1_1_feature_vector.html#ac0a4fceef14eba9efb471f5ec0476579" title="Synonym for SetWeight.">SetValue</a>(<span class="keyword">const</span> K &amp;uid, V new_value) {
<a name="l00296"></a>00296     <span class="keywordflow">return</span> <a class="code" href="classreranker_1_1_feature_vector.html#ad94d388ef81023981d6924a2020268d2" title="Sets the weight of the specified feature to the specified value.">SetWeight</a>(uid, new_value);
<a name="l00297"></a>00297   }
<a name="l00298"></a>00298 
<a name="l00303"></a><a class="code" href="classreranker_1_1_feature_vector.html#a0535e2a994950ccd367a47fde39faa98">00303</a>   <a class="code" href="classreranker_1_1_feature_vector.html" title="A class to represent a feature vector, where features are represented by unique identifiers, and feature values are represented by the template type.">FeatureVector&lt;K,V&gt;</a> &amp;<a class="code" href="classreranker_1_1_feature_vector.html#a0535e2a994950ccd367a47fde39faa98" title="Multiplies this vector, in situ, by the specified scalar.">Scale</a>(V scalar) {
<a name="l00304"></a>00304     <span class="keywordflow">for</span> (iterator it = features_.begin(); it != features_.end(); ++it) {
<a name="l00305"></a>00305       it-&gt;second = it-&gt;second * scalar;
<a name="l00306"></a>00306     }
<a name="l00307"></a>00307     <span class="keywordflow">return</span> *<span class="keyword">this</span>;
<a name="l00308"></a>00308   }
<a name="l00309"></a>00309 
<a name="l00320"></a><a class="code" href="classreranker_1_1_feature_vector.html#a76c6244a176999e72a54c78726ee718f">00320</a>   <a class="code" href="classreranker_1_1_feature_vector.html" title="A class to represent a feature vector, where features are represented by unique identifiers, and feature values are represented by the template type.">FeatureVector&lt;K,V&gt;</a> &amp;<a class="code" href="classreranker_1_1_feature_vector.html#a76c6244a176999e72a54c78726ee718f" title="Modifies this vector so that it equals this vector plus the scaled specified vector.">AddScaledVector</a>(<span class="keyword">const</span> <a class="code" href="classreranker_1_1_feature_vector.html" title="A class to represent a feature vector, where features are represented by unique identifiers, and feature values are represented by the template type.">FeatureVector&lt;K,V&gt;</a> &amp;other,
<a name="l00321"></a>00321                                       V scalar) {
<a name="l00322"></a>00322     <span class="keywordflow">for</span> (<a class="code" href="classreranker_1_1_feature_vector.html#a8dc4093ae778bffb30d2840002f02ba1" title="The type of const iterator for the feature-weight pairs in this vector.">const_iterator</a> it = other.<a class="code" href="classreranker_1_1_feature_vector.html#ae0c185e646996daff25fcad9b224eda5" title="Returns a const iterator pointing to the first of the feature-value pairs of this feature vector...">begin</a>(); it != other.<a class="code" href="classreranker_1_1_feature_vector.html#a9f2539faf4e600c2f75b613763d49fcd" title="Returns a const iterator pointing to the end of the feature-value pairs of this feature vector...">end</a>(); ++it) {
<a name="l00323"></a>00323       <a class="code" href="classreranker_1_1_feature_vector.html#a541f26184a4407f62eb5dad8102cf375" title="Increments the weight of the specified feature by the specified amount.">IncrementWeight</a>(it-&gt;first, scalar * it-&gt;second);
<a name="l00324"></a>00324     }
<a name="l00325"></a>00325     <span class="keywordflow">return</span> *<span class="keyword">this</span>;
<a name="l00326"></a>00326   }
<a name="l00327"></a>00327 
<a name="l00344"></a>00344   <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Collection&gt;
<a name="l00345"></a><a class="code" href="classreranker_1_1_feature_vector.html#aa732c2a9d4b249b652f881eb57112e1f">00345</a>   <a class="code" href="classreranker_1_1_feature_vector.html" title="A class to represent a feature vector, where features are represented by unique identifiers, and feature values are represented by the template type.">FeatureVector&lt;K,V&gt;</a> &amp;<a class="code" href="classreranker_1_1_feature_vector.html#aa732c2a9d4b249b652f881eb57112e1f" title="Modifies this vector so that it equals this vector plus the scaled specified subvector.">AddScaledSubvector</a>(<span class="keyword">const</span> Collection &amp;feature_uids,
<a name="l00346"></a>00346                                          <span class="keyword">const</span> <a class="code" href="classreranker_1_1_feature_vector.html" title="A class to represent a feature vector, where features are represented by unique identifiers, and feature values are represented by the template type.">FeatureVector&lt;K,V&gt;</a> &amp;
<a name="l00347"></a>00347                                          feature_vector,
<a name="l00348"></a>00348                                          V scalar) {
<a name="l00349"></a>00349     <span class="keywordflow">for</span> (<span class="keyword">typename</span> Collection::const_iterator it = feature_uids.begin();
<a name="l00350"></a>00350          it != feature_uids.end();
<a name="l00351"></a>00351          ++it) {
<a name="l00352"></a>00352       <a class="code" href="classreranker_1_1_feature_vector.html#a541f26184a4407f62eb5dad8102cf375" title="Increments the weight of the specified feature by the specified amount.">IncrementWeight</a>(*it, feature_vector.<a class="code" href="classreranker_1_1_feature_vector.html#a0786ae9b9f6b579a80d8ae601edfd80a" title="Returns the weight of the feature with the specified uid, where crucially features not &quot;present&quot; in t...">GetWeight</a>(*it) * scalar);
<a name="l00353"></a>00353     }
<a name="l00354"></a>00354     <span class="keywordflow">return</span> *<span class="keyword">this</span>;
<a name="l00355"></a>00355   }
<a name="l00356"></a>00356 
<a name="l00364"></a>00364   <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Collection&gt;
<a name="l00365"></a><a class="code" href="classreranker_1_1_feature_vector.html#afb07b55b5c08d60b2390c20b12ba1a57">00365</a>   <a class="code" href="classreranker_1_1_feature_vector.html" title="A class to represent a feature vector, where features are represented by unique identifiers, and feature values are represented by the template type.">FeatureVector&lt;K,V&gt;</a> &amp;<a class="code" href="classreranker_1_1_feature_vector.html#afb07b55b5c08d60b2390c20b12ba1a57" title="Increments the weights for the specified collection of features.">IncrementWeights</a>(<span class="keyword">const</span> Collection &amp;feature_uids,
<a name="l00366"></a>00366                                        V increment) {
<a name="l00367"></a>00367     <span class="keywordflow">for</span> (<span class="keyword">typename</span> Collection::const_iterator it = feature_uids.begin();
<a name="l00368"></a>00368          it != feature_uids.end();
<a name="l00369"></a>00369          ++it) {
<a name="l00370"></a>00370       <a class="code" href="classreranker_1_1_feature_vector.html#a541f26184a4407f62eb5dad8102cf375" title="Increments the weight of the specified feature by the specified amount.">IncrementWeight</a>(*it, increment);
<a name="l00371"></a>00371     }
<a name="l00372"></a>00372     <span class="keywordflow">return</span> *<span class="keyword">this</span>;
<a name="l00373"></a>00373   }
<a name="l00374"></a>00374 
<a name="l00377"></a><a class="code" href="classreranker_1_1_feature_vector.html#a7082611256f0b6dc90ba4b38cc43e4ab">00377</a>   <span class="keywordtype">void</span> <a class="code" href="classreranker_1_1_feature_vector.html#a7082611256f0b6dc90ba4b38cc43e4ab" title="Sets all feature weights to zero and, because this is a sparse vector, clears all storage...">clear</a>() {
<a name="l00378"></a>00378     features_.clear();
<a name="l00379"></a>00379   }
<a name="l00380"></a>00380 
<a name="l00381"></a>00381   <span class="comment">// I/O methods</span>
<a name="l00382"></a>00382 
<a name="l00383"></a>00383   <span class="comment">// TODO(dbikel): Add methods to serialize and de-serialize to/from protobuf&#39;s.</span>
<a name="l00384"></a>00384 
<a name="l00385"></a><a class="code" href="classreranker_1_1_feature_vector.html#ab17a2e88eac7592792ebdb62b731426b">00385</a>   <span class="keyword">friend</span> ostream &amp;operator&lt;&lt;(ostream &amp;os, const FeatureVector&lt;K,V&gt; &amp;fv) {
<a name="l00386"></a>00386     os &lt;&lt; <span class="stringliteral">&quot;[&quot;</span>;
<a name="l00387"></a>00387     <a class="code" href="classreranker_1_1_feature_vector.html#a8dc4093ae778bffb30d2840002f02ba1" title="The type of const iterator for the feature-weight pairs in this vector.">const_iterator</a> it = fv.features_.begin();
<a name="l00388"></a>00388     <span class="keywordflow">if</span> (it != fv.features_.end()) {
<a name="l00389"></a>00389       os &lt;&lt; it-&gt;first &lt;&lt; <span class="stringliteral">&quot;=&quot;</span> &lt;&lt; it-&gt;second;
<a name="l00390"></a>00390       ++it;
<a name="l00391"></a>00391     }
<a name="l00392"></a>00392     <span class="keywordflow">for</span> ( ; it != fv.features_.end(); ++it) {
<a name="l00393"></a>00393       os &lt;&lt; <span class="stringliteral">&quot; &quot;</span> &lt;&lt; it-&gt;first &lt;&lt; <span class="stringliteral">&quot;=&quot;</span> &lt;&lt; it-&gt;second;
<a name="l00394"></a>00394     }
<a name="l00395"></a>00395     os &lt;&lt; <span class="stringliteral">&quot;]&quot;</span>;
<a name="l00396"></a>00396     <span class="keywordflow">return</span> os;
<a name="l00397"></a>00397   }
<a name="l00398"></a>00398 
<a name="l00399"></a>00399  <span class="keyword">private</span>:
<a name="l00400"></a>00400   <span class="comment">// a helpful internal typedef for mutating methods</span>
<a name="l00401"></a>00401   <span class="keyword">typedef</span> <span class="keyword">typename</span> FeatureMap::iterator iterator;
<a name="l00402"></a>00402 
<a name="l00403"></a>00403   <span class="comment">// data members</span>
<a name="l00406"></a>00406 <span class="comment"></span>  <a class="code" href="classreranker_1_1_feature_vector.html#acf6076a07743206afbc35acf99a8f2df" title="The underlying type that this class stores the mapping of feature uid&#39;s to their weights.">FeatureMap</a> features_;
<a name="l00407"></a>00407 };
<a name="l00408"></a>00408 
<a name="l00409"></a>00409 }  <span class="comment">// namespace reranker</span>
<a name="l00410"></a>00410 
<a name="l00411"></a>00411 <span class="preprocessor">#endif</span>
</pre></div></div><!-- contents -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark">&#160;</span>Defines</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>



<hr class="footer"/><address class="footer"><small>
Generated on Tue Apr 9 2013 11:56:29 for Reranker Framework (ReFr) by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.6.1
</small></address>

</body>
</html>
